package dropouts

import org.springframework.dao.DataIntegrityViolationException
import groovy.time.TimeCategory

class QuotationController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
		Date xdate = new Date()

		redirect(action: "list", params: params)
    }

    def list() {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def cri = Quotation.createCriteria()
		def results = cri.list (params) {
			between('quotation_date', params.listfrom, params.listto)
		}
		flash.listfrom = params.listfrom
		flash.listto = params.listto
        [quotationInstanceList: results, quotationInstanceTotal: Quotation.count()]
    }

	def relist() {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		
		
		
		[quotationInstanceList: Quotation.list(params), quotationInstanceTotal: Quotation.count()]
	}
	
    def create() {
        [quotationInstance: new Quotation(params)]
    }

    def save() {
        def quotationInstance = new Quotation(params)
        if (!quotationInstance.save(flush: true)) {
            render(view: "create", model: [quotationInstance: quotationInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'quotation.label', default: 'Quotation'), quotationInstance.id])
        redirect(action: "show", id: quotationInstance.id)
    }

    def show() {
        def quotationInstance = Quotation.get(params.id)
        if (!quotationInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'quotation.label', default: 'Quotation'), params.id])
            redirect(action: "list")
            return
        }

        [quotationInstance: quotationInstance]
    }

    def edit() {
        def quotationInstance = Quotation.get(params.id)
        if (!quotationInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'quotation.label', default: 'Quotation'), params.id])
            redirect(action: "list")
            return
        }

        [quotationInstance: quotationInstance]
    }

    def update() {
        def quotationInstance = Quotation.get(params.id)
        if (!quotationInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'quotation.label', default: 'Quotation'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (quotationInstance.version > version) {
                quotationInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'quotation.label', default: 'Quotation')] as Object[],
                          "Another user has updated this Quotation while you were editing")
                render(view: "edit", model: [quotationInstance: quotationInstance])
                return
            }
        }

        quotationInstance.properties = params

        if (!quotationInstance.save(flush: true)) {
            render(view: "edit", model: [quotationInstance: quotationInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'quotation.label', default: 'Quotation'), quotationInstance.id])
        redirect(action: "show", id: quotationInstance.id)
    }

    def delete() {
        def quotationInstance = Quotation.get(params.id)
        if (!quotationInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'quotation.label', default: 'Quotation'), params.id])
            redirect(action: "list")
            return
        }

        try {
            quotationInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'quotation.label', default: 'Quotation'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'quotation.label', default: 'Quotation'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
